/*
 * Copyright (c) 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.services.samples.calendar.cmdline;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.DataStoreFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.CalendarScopes;

import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.services.calendar.model.*;
import com.google.api.client.util.DateTime;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.TimeZone;
import java.lang.Boolean;

/**
 * Main class for the Calendar API command line sample.
 * Demonstrates how to make an authenticated API call using OAuth 2 helper classes.
 */
public class CalendarSample {

  /**
   * Be sure to specify the name of your application. If the application name is {@code null} or
   * blank, the application will log a warning. Suggested format is "MyCompany-ProductName/1.0".
   */
  private static final String APPLICATION_NAME = "SCHEDULEme";

  /** Directory to store user credentials. */
  private static final java.io.File DATA_STORE_DIR =
      new java.io.File(System.getProperty("user.home"), ".store/calendar_sample");

  /**
   * Global instance of the {@link DataStoreFactory}. The best practice is to make it a single
   * globally shared instance across your application.
   */
  private static FileDataStoreFactory dataStoreFactory;

  /** Global instance of the JSON factory. */
  private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

  /** Global instance of the HTTP transport. */
  private static HttpTransport httpTransport;

  @SuppressWarnings("unused")
  private static Calendar client;

  /** Authorizes the installed application to access user's protected data. */
  private static Credential authorize() throws Exception {
    // load client secrets
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
        new InputStreamReader(CalendarSample.class.getResourceAsStream("/client_secrets.json")));
    if (clientSecrets.getDetails().getClientId().startsWith("Enter") ||
        clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
      System.out.println(
          "Overwrite the src/main/resources/client_secrets.json file with the client secrets file "
          + "you downloaded from the Quickstart tool or manually enter your Client ID and Secret "
          + "from https://code.google.com/apis/console/?api=calendar#project:114567617397 "
          + "into src/main/resources/client_secrets.json");
      System.exit(1);
    }

    // Set up authorization code flow.
    // Ask for only the permissions you need. Asking for more permissions will
    // reduce the number of users who finish the process for giving you access
    // to their accounts. It will also increase the amount of effort you will
    // have to spend explaining to users what you are doing with their data.
    // Here we are listing all of the available scopes. You should remove scopes
    // that you are not actually using.
    Set<String> scopes = new HashSet<String>();
    scopes.add(CalendarScopes.CALENDAR);
	scopes.add(CalendarScopes.CALENDAR_READONLY);

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
        httpTransport, JSON_FACTORY, clientSecrets, scopes)
        .setDataStoreFactory(dataStoreFactory)
        .build();
    // authorize
    return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
  }

  public static void main(String[] args) {
    try {
      // initialize the transport
      httpTransport = GoogleNetHttpTransport.newTrustedTransport();

      // initialize the data store factory
      dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

      // authorization
      Credential credential = authorize();

      // set up global Calendar instance
      client = new Calendar.Builder(httpTransport, JSON_FACTORY, credential)
          .setApplicationName(APPLICATION_NAME).build();

      System.out.println("Success! Now add code here.");

	String calendarId = "scheduleme514@gmail.com";

	/* Create a new event */
	Event event = new Event();

	event.setSummary("Appointment"); // event title
	event.setLocation("Somewhere");  // location
	event.setColorId("3");	// color
	event.setDescription("When?Where?Why?How?");

	// add attendees
	ArrayList<EventAttendee> attendees = new ArrayList<EventAttendee>();
	attendees.add(new EventAttendee().setEmail("attendee1@test.com"));
	event.setAttendees(attendees);

	// set start date-time, end date-time
	DateTime start = DateTime.parseRfc3339("2013-10-09T10:00:00.000-05:00");
	DateTime end = DateTime.parseRfc3339("2013-10-09T10:25:00.000-05:00");
	event.setStart(new EventDateTime().setDateTime(start).setTimeZone("America/Chicago"));
	event.setEnd(new EventDateTime().setDateTime(end).setTimeZone("America/Chicago"));
	// set repeating rule
	event.setRecurrence(Arrays.asList("RRULE:FREQ=WEEKLY;UNTIL=20131207T102500Z"));
	// set reminders
	EventReminder eventReminder1 = new EventReminder();
	EventReminder eventReminder2 = new EventReminder();
	eventReminder1.setMethod("email");
	eventReminder1.setMinutes(120);
	eventReminder2.setMethod("popup");
	eventReminder2.setMinutes(60);
	Event.Reminders eventReminders = new Event.Reminders();
	eventReminders.setUseDefault(new Boolean("False"));
	eventReminders.setOverrides(Arrays.asList(eventReminder1, eventReminder2));
	event.setReminders(eventReminders);

	// Insert an event to Google Calendar
	Event createdEvent = client.events().insert(calendarId, event).execute();
	System.out.println("event title: " + createdEvent.getSummary());
	System.out.println("color id: " + createdEvent.getColorId());
	System.out.println("event id: " + createdEvent.getId());

	// get an event
	Event getEvent = client.events().get(calendarId, createdEvent.getId()).execute();
	System.out.println("get a event: " + getEvent.getSummary());

/* delete a event */
//	client.events().delete(calendarId, createdEvent.getId()).execute();

/* update a event */
/*
	createdEvent.setSummary("Updated Event");
	createdEvent.setColorId("4");	
	Event updatedEvent = client.events().update("calendarId", createdEvent.getId(), createdEvent).execute();
	System.out.println("Updated: ");
	System.out.println("event title: " + updatedEvent.getSummary());
	System.out.println("color id: " + updatedEvent.getColorId());
	System.out.println("event id: " + updatedEvent.getId());
*/


/* list all event title, event id */
/*
	String pageToken = null;
	do {
	  Events events = client.events().list("primary").setPageToken(pageToken).execute();
	  List<Event> items = events.getItems();
	  for (Event eventEntry : items) {
	    System.out.println(eventEntry.getSummary());
	    System.out.println(eventEntry.getId());
	  }
	  pageToken = events.getNextPageToken();
	} while (pageToken != null);
*/

    System.out.println("Success! ");

    } catch (IOException e) {
      System.err.println(e.getMessage());
    } catch (Throwable t) {
      t.printStackTrace();
    }
    System.exit(1);
  }
}
